package com.cqq.common.controller;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ui.ModelMap;

public abstract class BaseController extends com.cqq.common.core.base.BaseController{
	protected static final Logger log = LoggerFactory.getLogger(BaseController.class);

    protected static final String MESSAGE_SUCCESS="message_success";
    
    protected static final String MESSAGE_ERROR="message_error";
    
    protected static final String MESSAGE_INFO="message_info";
    
    protected static final String MESSAGE_WARNING="message_warning";
    
    protected static final String MESSAGE_CODE_SUCCESS="success";
    
    protected static final String MESSAGE_CODE_ERROR="error";
    
    /*
	@Resource
	private MessageUtil messageUtil;
	
	protected <T extends BaseException> T buildException(T e) {
		String msg=this.messageUtil.getMessage(e.getErrorCode());
		log.error(msg);
		e.setErrorMsg(msg);
		if(e.getCaused()!=null){
			log.error(e.getMessage(),e.getCaused());
		}
		return e;
	}*/
    
    @SuppressWarnings({ "unused", "unchecked" })
    public Map<String,Object>  getRequestMap(final HttpServletRequest httpServletRequest) {
		Map<String,Object> parameterMap = new HashMap<String,Object>();
		for (Object o : httpServletRequest.getParameterMap().entrySet()) {
			Entry<String, String[]> e = (Entry<String, String[]>) o;
			String key = e.getKey();
			String[] v = e.getValue();
			parameterMap.put(key, StringUtils.join(v, ","));
		}
		putPageToMap(parameterMap);
		return parameterMap;
	}
    
    public void putPageToMap(Map<String,Object> parameterMap){
    	if(null == parameterMap){
    		return;
    	}
    	  int pageSize = 10;
    	  int currentPage=1;
    	  int offset=0;
    	  if(parameterMap.containsKey(Constant.CURRENT_PAGE_NAME)){
	             currentPage = Integer.parseInt((String)parameterMap.get(Constant.CURRENT_PAGE_NAME));
	             currentPage=currentPage==0?1:currentPage;
	             offset = (currentPage - 1) * pageSize;
	        }
    	  parameterMap.put(Constant.PAGE_SIZE_NAME, pageSize);
          parameterMap.put(Constant.CURRENT_PAGE_NAME, currentPage);
          parameterMap.put(Constant.OFFSET_NAME, offset);
    }
    
    public void putModelMap(ModelMap map,Map<String,Object> parameterMap){
    	putPageCount(parameterMap);
    	map.putAll(parameterMap);
    }
    
    public void putPageCount(Map<String,Object> parameterMap) {
    	if(null == parameterMap){
    		return;
    	}
    	
    	if(!parameterMap.containsKey(Constant.COUNT_NAME)){
    		return;
    	}
    	if(!parameterMap.containsKey(Constant.PAGE_SIZE_NAME)){
    		return;
    	}
    	
    	int count = (Integer)parameterMap.get(Constant.COUNT_NAME);
    	int pageSize =(Integer)parameterMap.get(Constant.PAGE_SIZE_NAME);
    	
		int mod = count % pageSize;
		int pageCount = 0;
		if (mod > 0) {
			pageCount = count / pageSize + 1;
		} else if (mod == 0) {
			pageCount = count / pageSize;
		}
		if (pageCount == 0) {
			pageCount++;
		}
		parameterMap.put(Constant.TOTAL_PAGE_NAME, pageCount);
	}
}